<?php

   class CommonAction extends Action{
   
      public function _initialize(){
	  
	     
		/*	if(isset($_GET['pid'])){
			
			  session('pid',$_GET['pid']);
			}
		 
		    if(isset($_GET['uid'])){
			
			  session('uid',$_GET['uid']);
			}
			
			if(isset($_GET['id'])){
			
			   session('id',$_GET['id']);
			
			}
			
			
		    if(isset($_GET['str'])){
		  
				   $end=time()-300;
				   $where['time']=array('gt',$end);
				   $where['str']=array('eq',$str);
				   $ishave=M('suijizifu')->where($where)->find();
				   if($ishave==null){
					 $sys=M('system')->where('id=1')->find();
					 $url=$sys['zhengguiwangzhi'];
					 Header("Location:$url");
				   }
		     }
		 */
		 
		   // session('id',$_GET['id']);
		   
		  
	     
	  }
	  
	  //分页类  table 表名，num 每页显示条数
	  public function listpage($table,$num,$order='',$where=''){
	    
		   $pagemodel=M($table);
		   import('ORG.Util.Page');
		   $count=$pagemodel->where($where)->count();
		   $page=new page($count,$num);
		   $limit=$page->firstRow.','.$page->listRows;
		   $re=$pagemodel->order($order)->limit($limit)->where($where)->select();
		   $this->re=$re;
		   $this->page=$page->show();
		 
	  }
	  
	  
	    public function page($table,$num,$order='',$where=''){
	    
		   $pagemodel=M($table);
		   import('ORG.Util.Page');
		   $count=$pagemodel->where($where)->count();
		   $page=new page($count,$num);
		   $limit=$page->firstRow.','.$page->listRows;
		   $re=$pagemodel->order($order)->limit($limit)->where($where)->select();
		   
		   $this->page=$page->show();
		   
		   return $re;
		 
	  }
	  
	  //图片上传类
	  
	  public function upload($path){
	      
	        import('ORG.Net.UploadFile');
		    $config['savePath']       = "$path";
			// $config['thumb']          = true;
		 //    $config['thumbPrefix']    = 'm_,s_';
		 //    $config['thumbMaxWidth']  = "$big,$small";
		 //    $config['thumbMaxHeight'] = "$big,$small";
			$upload = new UploadFile($config);
            if($upload->upload()){
		    $info=$upload->getUploadFileInfo();
		    }else{
			$upload->getErrorMsg();
			}
			
			return $info;
	    
	  }
	  
	  //找出所有栏目 id 顶级栏目ID
	  
	  
	  
	    public function sel($cata,$mark='--',$pid=0,$level=0){
	  
	     $arr=array();
		 
		 foreach($cata as $v ){
		   
		    if($v['topid']==$pid){
			    $v['level']=$level+1;
			   $v['mark']=str_repeat($mark,$level);
			   $arr[]=$v;
			   $arr=array_merge($arr,$this->sel($cata,$mark,$v['id'],$level+1));
			}
		  }
		 return $arr;
	  }
	  
	  
	    public function sonid($cata,$pid){
  
      $arr=array();
	 // dump($cata);die;
	  foreach($cata as $v){
	    
	     if($v['topid']==$pid){
		   $arr[]=$v['id'];
		  
		   $arr=array_merge($arr,$this->sonid($cata,$v['id']));
		   
		 }
		 //dump($v);
	     
	  }
	 // dump($arr);
    return $arr;
  
  }
  //图片二进制编码字符串
  public function pic64($name){
  	       $open    = fopen("./images/".$name,'rb');
           $content = fread($open,filesize("./images/".$name));
           fclose($open);
           $re = base64_encode($content);
           return $re;
  }


  //百度色情图片验证方法
  public function picyanzheng($content){
	  	$ch = curl_init();
	    $url = 'http://apis.baidu.com/idl_baidu/pornfilter/pornfilter';
	    $header = array(
	        'Content-Type:application/x-www-form-urlencoded',
	        'apikey:fac92425e9f342f1e4533a53cc3c5952',
	    );
	    $data = '{
	    "params": [
	        {
	        	"image":"'.$content.'",
	            "versionnum": "1.0",
	            "logid": 1,
	            "cmdid": "cmdid",
	            "appid": "fac92425e9f342f1e4533a53cc3c5952",
	            "type": "type"
	        }
	    ],
	    "jsonrpc": "2.0",
	    "method": "classify",
	    "id": 12345
	   }';
	    // 添加apikey到header
	    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
	    // 添加参数
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    // 执行HTTP请求
	    curl_setopt($ch , CURLOPT_URL , $url);
	    $res = curl_exec($ch);

	    $arr = json_decode($res,true);
	    return $arr['result']['_ret']['confidence_level'];
  }
  //高斯模糊处理
     public function picblur($source, $dest, $deep){
     	   // 判断原图是否存在
	    if(!file_exists($source)){
	        return false;
	    }

	    // 获取原图信息
	   list($owidth, $oheight, $otype) = getimagesize($source);
	    // $arr = getimagesize($source);
	    // var_dump($arr);exit();

	    switch($otype){
	        case 1: $source_img = imagecreatefromgif($source); break;
	        case 2: $source_img = imagecreatefromjpeg($source); break;
	        case 3: $source_img = imagecreatefrompng($source); break;
	        default:
	            return false;
	    }
	   //var_dump($source_img);exit();
	    // 模糊图片
	    for($x=0; $x<$deep; $x++){
	        imagefilter($source_img, IMG_FILTER_GAUSSIAN_BLUR);
	    }

	    // 生成图片
	    switch($otype){
	        case 1: imagegif($source_img, $dest); break;
	        case 2: imagejpeg($source_img, $dest); break;
	        case 3: imagepng($source_img, $dest); break;
	    }

	    return is_file($dest)? true : false;
     } 
     //模板消息,token,图片所有者openid,打赏时间，打赏者昵称，打赏钱数
     public function moban($token,$openid,$time,$who,$money){
     	    $url  = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
      $data = '{
                "touser":"'.$openid.'",
                "template_id":"d_zC533yaUQ0jDKnWxL3hp4HXkzi3MdEyjk92klFtiM",
                "url":"",
                "topcolor":"#FF0000",
                "data":{
                "first": {
                "value":"'.$who.'打赏了你的照片，赏金￥'.$money.'",
                "color":"#173177"
                },
                "keyword1":{
                "value":"打赏照片",
                "color":"#173177"
                },
                "keyword2":{
                "value":"'.$time.'",
                "color":"#173177"
                },
                "remark":{
                "value":"祝您生活幸福哦",
                "color":"#173177"
                }    
                }
             }';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST,1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
     }
	   
   
   }